/*
 * 演示多态数组
 * 创建一个 Person 对象
 * 创建一个 Student 对象
 * 创建一个 Teacher 对象
 * 统一放在数组里，并调用每个对象的 mission() 方法。
 */

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    // getter 和 setter
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    // mission() 方法
    public String mission() {
        return name + "\t" + "要开开心心";
    }
}

class Student extends Person {
    private double score;

    public Student(String name, double score) {
        super(name);
        this.score = score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    //重写父类的say方法
    @Override
    public String mission() {
        return super.mission() + " score =" + score + " 要好好学习！";
    }
}

class Teacher extends Person {
    private double salary;

    public Teacher(String name, double salary) {
        super(name);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    //重写父类的 mission 方法
    @Override
    public String mission() {
        return super.mission() + " salary =" + salary + " 要好好教书！";
    }
}

public class text {
    public static void main(String[] args) {
        Person[] persons = new Person[3];
        persons[0] = new Person("小汤");
        persons[1] = new Student("小韬", 100);
        persons[2] = new Teacher("小蒲", 1000);

        //循环遍历多态数组，调用 mission
        for(int i = 0; i < persons.length; i++) {
            //此处涉及动态绑定机制
            // Person[i] 编译类型是 Person ,运行类型根据实际情况由 JVM 判断
            System.out.println(persons[i].mission());
        }
    }
}
